/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Lenovo
 * Date: 2025-04-16
 * Time: 6:31
 */
public class Main {
    public static void main(String[] args) {
        // 指定泛型类型为String
        MyArray<String> myArray = new MyArray<>();
        myArray.setVal(0, "hello");
        myArray.setVal(1, "world");

        //myArray.setVal(2, 100); // 编译报错！java: 不兼容的类型: int无法转换为java.lang.String。无法存入Integer

        // 指定泛型类型为Integer
        MyArray<Integer> myArray2 = new MyArray<>();
        myArray2.setVal(0,12);
        myArray2.setVal(1,20);

        // 直接赋值，无需强制转换
        String ret = myArray.getPos(1); // 编译器确保返回类型是String
        System.out.println(ret); // 输出：world


        Integer ret2 = myArray2.getPos(0); // 编译器确保返回类型是Integer
        Integer ret3 = myArray2.getPos(1);
        System.out.println(ret2);// 输出：12
        System.out.println(ret3);// 输出：20
    }
}
